<!--
 * @Author: wangming
 * @Date: 2021-04-15 15:38:32
 * @LastEditors: wangming
 * @LastEditTime: 2022-08-03 11:47:35
 * @Description: 数据字段
-->
<template>
  <el-form-item
    label="数据字段"
    prop="modelName"
    :rules="[
      {
        required: true,
        message: '数据字段不能为空',
        trigger: 'change',
      },
    ]"
  >
    <el-select
      @focus="allowcreate = true"
      @change="changeModel"
      :value="value.modelName"
      placeholder="请选择"
      size="small"
      class="mb10"
      style="width: 100%"
      filterable
      :allow-create="allowcreate"
    >
      <el-option
        :disabled="ModelKeyList.indexOf(item.code) > -1"
        v-for="(item, index) in myEntitymodelList"
        :key="index"
        :label="`${item.name}【${item.code}】`"
        :value="item.code"
      >
      </el-option>
    </el-select>
  </el-form-item>
</template>

<script>
import basicmixins from "./basicmixins";
import { mapGetters } from "vuex";
import { getHmmodelModelcode } from "@/apis/paas/HmModelController.js";
const HLENUM = {
  //控件可选短字段过滤
  hlInput: "SHORT_TEXT,SEQ", //短文本
  hlPassword: "SHORT_TEXT", //密码
  hlTextarea: "LONG_TEXT,SHORT_TEXT", //长文本
  hlInputNumber: "NUMBER,INTEGER", //数值
  hlMoneyInput: "NUMBER", //金额
  hlCalculation: "NUMBER", //计算属性
  hlModelSelect: "SHORT_TEXT", //字段选择
  hlSelect: "RELATION_SELECT,RELATION_REFERENCE", //下啦选择
  hlRadio: "RELATION_REFERENCE", //单选
  hlCheckbox: "RELATION_SELECT", //多选
  hlSwitch: "BOOL", //开关
  hlDatePicker: "DATE", //日期
  hlTimePicker: "TIME", //时间
  hlFileImage: "PICTURE", //图片
  hlFile: "ATTACHMENT", //附件
  hlPersonnel: "STAFF", //人员
  hlDepartment: "DEPARTMENT", //部门
  hlTableAdd: "SUB_TABLE", //字表
  hlChildTb: "SUB_TABLE",
  hlSelectTree: "RELATION_SELECT,PARENT_ID,RELATION_REFERENCE", //下拉tree
  hlProcess: "LONG_TEXT", //工序图
  hlCustom:
    "SHORT_TEXT,LONG_TEXT,NUMBER,RELATION_SELECT,RELATION_REFERENCE,DATE,TIME", //自定义
  hlCascader: "RELATION_SELECT,PARENT_ID",
  hlQrcodeDecoder: "SHORT_TEXT",
  SearchInput: "SHORT_TEXT,SEQ,LONG_TEXT",
  SearchNumberInput: "NUMBER,INTEGER",
  SearchDatePicker: "DATE",
  SearchTimePicker: "TIME",
  SearchSelect: "RELATION_SELECT,PARENT_ID,RELATION_REFERENCE",
  SearchStaffSelect: "STAFF",
  SearchDepartmentSelect: "DEPARTMENT",
  SearchCascader: "RELATION_SELECT,PARENT_ID",
  SearchTreeSelect: "RELATION_SELECT,PARENT_ID,RELATION_REFERENCE",
  hlColorPicker: "SHORT_TEXT",
};
export default {
  computed: {
    ...mapGetters(["getFormList", "getEntitymodelList"]),
    myEntitymodelList() {
      return this.getEntitymodelList.filter(
        (i) =>
          HLENUM[this.value.tagName].indexOf(i.type) > -1 &&
          (this.value.isSearchCom ? true : !i.builtIn) &&
          this.checkedTree(i.relationType, i.type)
      );
    },
    ModelKeyList() {
      return this.getFormList
        .filter((i) => !!i.modelName)
        .map(({ modelName }) => modelName);
    },
  },
  mixins: [basicmixins],
  data() {
    return {
      allowcreate: false,
    };
  },
  methods: {
    checkedTree(checkedTree, type) {
      if (
        this.value.tagName === "hlSelectTree" ||
        this.value.tagName === "hlCascader"
      ) {
        return checkedTree === "BUSINESS" || type === "PARENT_ID";
      } else {
        return true;
      }
    },
    async changeModel(value) {
      this.value.modelName = value;
      //使得表单校验初始化的时候不触发非空校验
      this.value.__config.required = false;
      this.value.__config.disabled = false;
      await this.$nextTick();
      const field = this.myEntitymodelList.find(
        (field) => field.code === value
      );
      if (field) {
        const {
          name,
          nullable,
          length,
          type,
          config,
          relationType,
          modelCode,
          localism,
          defaultValue,
          defaultValueType,
        } = field;

        if (!!relationType) {
          //表单业务模型的code
          this.value.__config.modelKey = modelCode;
          //数据源查询所需字段
          this.value.__config.relationType = relationType;
        }
        try {
          if (!!config) {
            //被关联业务的code
            let { value, modelCode } = JSON.parse(config) || {};
            this.value.__config.valueTeam = value;
            if (!!modelCode) {
              this.value.__config.modelCode = modelCode;
              let { supportTree } = await getHmmodelModelcode({ modelCode });
              if (
                (this.value.tagName === "hlCascader" ||
                  this.value.tagName === "hlSelectTree") &&
                !supportTree
              ) {
                this.$message.warning("关联的模型不是树结构！");
              }
              if (
                this.value.tagName !== "hlCascader" &&
                this.value.tagName !== "hlSelectTree" &&
                supportTree
              ) {
                this.$message.warning("关联的模型是树结构！");
              }
            }
          }
        } catch (error) {}
        this.value.__config.label = name;
        if (!!this.value.__config.i18n) {
          this.$set(this.value.__config.i18n, "label", localism);
        } else {
          this.$set(this.value.__config, "i18n", { label: localism });
        }
        this.$set(this.value.__config, "codeType", type);
        this.value.__config.required = nullable === 0;
        this.value.__config.isNullable = nullable === 0;
        if (type === "SHORT_TEXT") {
          this.value.__config.inputmax = length;
          this.value.__config.maxlength = length;
        }
        if (type === "INTEGER") {
          this.$set(this.value.__config, "limitNumber", 0);
        }
        if (type === "SEQ") {
          //序列号的特殊逻辑 不必填且禁用
          this.value.__config.required = false;
          this.value.__config.isNullable = true;
          this.value.__config.disabled = true;
        }
        if (type === "PARENT_ID") {
          //父节点id逻辑
          this.value.__config.modelKey = modelCode;
          this.value.__config.modelCode = modelCode;
        }
        // 重置默认值
        // this.checkDefaultValue(type);
        // if (
        //   [
        //     "SHORT_TEXT",
        //     "LONG_TEXT",
        //     "NUMBER",
        //     "INTEGER",
        //     "DATE",
        //     "TIME",
        //     "BOOL",
        //     "RELATION_SELECT",
        //     "RELATION_REFERENCE",
        //   ].includes(type)
        // ) {
        //   if (defaultValue || defaultValueType) {
        //     if (type === "BOOL" && ["true", "false"].includes(defaultValue)) {
        //       const flag =
        //         defaultValue === "true"
        //           ? true
        //           : defaultValue === "false"
        //           ? false
        //           : null;
        //       this.value.defaultValue = flag;
        //     } else if (["NUMBER", "INTEGER"].includes(type)) {
        //       this.value.defaultValue = Number(defaultValue);
        //     } else if (type === "DATE") {
        //       if (defaultValueType === "SYSTEM_VALUE") {
        //         this.$set(this.value.__config, "isCurrentDate", true);
        //       } else {
        //         this.$set(this.value.__config, "isCurrentDate", false);
        //       }
        //     } else {
        //       console.log(222,this.value)
        //       if (this.value.defaultValue !== undefined) {
        //         this.value.defaultValue = defaultValue;
        //       }
        //     }
        //   }
        // }
      }
    },
  },
};
</script>

<style lang="scss" scoped></style>
